package logic

import (
	"an_ju_ke/models"
	"context"
	"fmt"
	"math/rand"
	"time"

	"an_ju_ke/myrpc/internal/svc"
	"an_ju_ke/myrpc/myrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsLogic {
	return &SmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsLogic) Sms(in *myrpc.SmsRequest) (*myrpc.SmsResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("sms_captcha_user_phone:%s", in.UserPhone)
	code := fmt.Sprintf("%d", rand.Intn(9000)+1000)
	//sms, err := pkg.Sms(code)
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf("短信发送失败")
	//}
	models.Rdb.Set(models.Ctx, key, code, 3*time.Minute)
	return &myrpc.SmsResponse{}, nil
}
